var storage = storages.create("autotouchShijieXy");
var delayBase = 1;
var yanchi = 500; // 点击后停顿
var yanchi1 = 1000; // 点击前停顿
var yanchi2 = 2000; // 点击后停顿
var yanchi3 = 3000; // 点击后停顿
var yanchi5 = 5000; // 点击后停顿
var deviceWidth = device.width > device.height ? device.width : device.height;
var deviceHeight = device.height > device.width ? device.width : device.height;
var picJizhun = deviceHeight / 1080;
var picJizhunHeight = deviceHeight / 1080;
var duiwushangxian = Boolean(storage.get("tianxuan", true)) ? 3 : 2;
var shangciweizhi = storage.get("shangciweizhi", { x: 0, y: 0 }); // 上次位置坐标
var xiaodituPosit = storage.get("xiaodituPosit", { x: 0, y: 0 }); // 小地图起始位置
var yuanbaoweizhi = storage.get("yuanbaoweizhi", { x: 2020, y: 0 }); // 元宝位置
var zhandouliWeizhi = storage.get("zhandouliPosit", { x: 0, y: 0, w: 500, h: 65 }); // 战斗力起始位置
var cunliangWeizhi = storage.get("cunliangWeizhi", { x: 0, y: 0, w: 260, h: 45 }); // 存量起始位置
var yingxiongMsgWeizhi = storage.get("yingxiongMsgWeizhi", { x: 0, y: 0, w: 300, h: 0 }); // 英雄信息坐标
var ziyuanZuobianWeizhi = storage.get("ziyuanZuobianWeizhi", { x: 0, y: 0, w: 300, h: 65 }); // 资源信息坐标 

function myLogFunc() {
    log(arguments)
}

if (deviceHeight == 720) {
    picJizhun = 0.666666666666667;
}
if (deviceHeight == 1152) { // 2376
    picJizhun = 1.0658; // 1.0666666666666667 // 1.093859999999997
    picJizhunHeight = 1.04471;
}
if (deviceHeight == 1200) {
    picJizhun = 1.12;
}


// picJizhunHeight = picJizhun;

shibiefangshi = 1


// let thisImgsN = images.read("./res/xuxiaohaochihou.png");
// thisImgsN = images.scale(thisImgsN, picJizhun, picJizhunHeight)
// let pN = images.matchTemplate(captureScreen(), thisImgsN, { threshold: 0.8 });
// thisImgsN.recycle();
// log(pN)


// 如果是第一次则配置战斗力
// if (true) {
//     myLogFunc('第一次配置斥候参数')
//     sleep(yanchi)
//     let thisImgsP = images.read("./res/xuxiaohaochihou.png");
//     thisImgsP = images.scale(thisImgsP, picJizhun, picJizhunHeight)
//     let pP = findImage(captureScreen(), thisImgsP, { threshold: 0.85 });
//     thisImgsP.recycle();
//     if (pP) {
//         log('斥候起点坐标', pP)
//         let newObj = { x: pP.x, y: pP.y, w: 300, h: 65 };
//         storage.put("xiaohaochihou", newObj);
//         xiaohaochihou = storage.get("xiaohaochihou", { x: 0, y: 0, w: 300, h: 65 });
//     } else {
//         toastLog("斥候信息异常")
//         exit()
//     }
// }

// let clip = images.clip(captureScreen(), xiaohaochihou.x, xiaohaochihou.y, xiaohaochihou.w * picJizhun, xiaohaochihou.h);
// let resultText = "";
// if (shibiefangshi == 0) {
//     resultText = gmlkit.ocr(clip, "zh");
//     resultText = resultText.text;
// } else if (shibiefangshi == 1) {
//     resultText = paddle.ocrText(clip);
//     resultText = resultText[0]
// } else {
//     resultText = gmlkit.ocr(clip, "zh");
//     resultText = resultText.text;
// }
// clip.recycle();

// log('resultText',resultText)


// let enter = true;
// while (enter) {
//     let thisImgs = images.read("./res/黑底关闭.png");
//     thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
//     let p = findImage(captureScreen(), thisImgs, { region: [(deviceWidth / 2), 0, (deviceWidth / 2), (deviceHeight / 2)], threshold: 0.88 });
//     thisImgs.recycle();
//     if (p) {
//         myLogFunc('关闭')
//         click(p.x + 5, p.y + 5);
//         sleep(yanchi1)
//     } else {
//         enter = false;
//         sleep(yanchi)
//     }
// }

// var youshangDuilie = { x: deviceWidth - (880 * picJizhun), y: (159 * picJizhun), w: (880 * picJizhun), h: 65 }
// log(youshangDuilie)
//  // 有天选先把能跳的跳了
//  let thisImgs = images.read("./res/jinru.png");
//  thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
//  let myThre = 0.7;
//  if (deviceHeight == 1080) {
//      myThre = 0.72;
//  } else {
//      myThre = 0.68;
//  }
//  log(myThre)
//  let result = images.matchTemplate(captureScreen(), thisImgs, { region: [youshangDuilie.x, youshangDuilie.y, youshangDuilie.w, youshangDuilie.h], threshold: myThre, max: 4 });
//  thisImgs.recycle();
//  log('result',result)



// let thisImgs2 = images.read("./res/联盟资源关闭.png");
//         thisImgs2 = images.scale(thisImgs2, picJizhun, picJizhunHeight)
//         // let p2 = findImage(captureScreen(), thisImgs2, { threshold: 0.86 });

//         let p2 = images.matchTemplate(captureScreen(), thisImgs2, { threshold: 0.6, max: 4 });
//         thisImgs2.recycle();
//     log(p2)
// if (p2) {
//     log(p2.point)
//     log(p2.point.x)
// }


// 324 84

// 304 80


lianmenghuzhuFunc()

// 联盟互助
function lianmenghuzhuFunc() {
    sleep(yanchi1)
    // 联盟采集流程
    while (!seeLianmengFunc()) {
        yichangFanhuiFunc("联盟互助")
        sleep(yanchi3)
    }
    sleep(yanchi1)
    let thisImgs = images.read("./res/联盟互助.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { region: [0, (deviceHeight / 2), (deviceWidth / 2), (deviceHeight / 2)], threshold: 0.8 });
    thisImgs.recycle();
    if (p) {
        myLogFunc('点击联盟互助')
        sleep(yanchi)
        click(p.x, p.y)
        sleep(yanchi1)

        let thisImgs1 = images.read("./res/全部帮助.png");
        thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
        let result = findImage(captureScreen(), thisImgs1, { region: [0, (deviceHeight / 2), deviceWidth, (deviceHeight / 2)], threshold: 0.85 });
        thisImgs1.recycle();
        if (result) {
            sleep(yanchi)
            click(result.x, result.y)
        }
        sleep(yanchi)
        back()
    } else {
        myLogFunc('error联盟互助')

        yichangFanhuiFunc("联盟互助异常")
        sleep(yanchi1)
        seeLianmengFunc()
        sleep(yanchi2)
        lianmengjianzhuFunc()
    }
}

// 联盟捐赠
function lianmengjuanzengFunc() {
    sleep(yanchi1)
    // 联盟采集流程
    while (!seeLianmengFunc()) {
        yichangFanhuiFunc("联盟捐赠")
        sleep(yanchi3)
    }
    sleep(yanchi1)
    let thisImgs = images.read("./res/联盟商店.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { region: [0, (deviceHeight / 2), (deviceWidth/2), (deviceHeight / 2)], threshold: 0.8 });
    thisImgs.recycle();
    if (p) {
        myLogFunc('点击联盟互助')
        sleep(yanchi)
        click(p.x, p.y)
        sleep(yanchi1)
        let enter = true;
        while (enter) {
            let thisImgs1 = images.read("./res/贡献.png");
            thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
            let result = findImage(captureScreen(), thisImgs1, { region: [0, 0, (deviceWidth/2), (deviceHeight - (220*picJizhun))], threshold: 0.85 });
            thisImgs1.recycle();
            if (result) {
                sleep(yanchi)
                click(result.x, result.y)
            }else{
                enter = false
            }
        }
        sleep(yanchi)
        back()
    } else {
        myLogFunc('error联盟互助')

        yichangFanhuiFunc("联盟互助异常")
        sleep(yanchi1)
        seeLianmengFunc()
        sleep(yanchi2)
        lianmengjianzhuFunc()
    }

}


// 查看联盟
function seeLianmengFunc() {
    sleep(yanchi1)
    let thisImgs = images.read("./res/联盟按钮.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { region: [0, (deviceHeight / 2), deviceWidth, (deviceHeight / 2)], threshold: 0.82 });
    thisImgs.recycle();
    if (p) {
        myLogFunc('点击联盟')
        sleep(yanchi)
        click(p.x, p.y)
        sleep(yanchi1)
        return true;
    } else {
        myLogFunc('error联盟按钮')
        return false;
    }
}











// 查看世界地图
function seeShijieditu() {
    let thisImgs = images.read("./res/世界地图按钮.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs);
    thisImgs.recycle();
    if (p) {
        myLogFunc('打开世界地图')
        click(p.x, p.y)
        sleep(yanchi1)

        // 如果是第一次则配置地图 700*600
        if (xiaodituPosit.x == 0) {
            myLogFunc('第一次配置小地图参数')
            sleep(yanchi1)
            let thisImgs1 = null;
            // if (Boolean(storage.get("isSanguo", false))) {
            //     thisImgs1 = images.read("./res/小地图1.png");
            // }else{
            //     thisImgs1 = images.read("./res/小地图.png");
            // }
            thisImgs1 = images.read("./res/小地图1.png");
            thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
            let p1 = findImage(captureScreen(), thisImgs1, { threshold: 0.9 });
            thisImgs1.recycle();
            if (p1) {
                log('小地图起点坐标', p1)
                let newObj = { x: p1.x, y: p1.y };
                storage.put("xiaodituPosit", newObj);
                xiaodituPosit = storage.get("xiaodituPosit", { x: 0, y: 0 });
            } else {
                toastLog("小地图信息异常")
                exit()
            }
        }
    } else {
        myLogFunc('打开地图失败')
        sleep(yanchi)
    }
}

// 前往指定坐标 483就是16 进阶2.82 需要算法支持各种分辨率
function goXyFunc(x, y) {
    myLogFunc('前往坐标点', x, y)
    x = Number(x)
    y = Number(y)
    let zhia = parseInt(x / 50) + 1;
    let jiaozheng = 3;
    if (x < 127) {
        jiaozheng = 2;
        //     zhia = zhia - 1;
    }
    let xx = (xiaodituPosit.x + jiaozheng) + ((x + zhia) * (2.754 * picJizhun))
    let yy = (xiaodituPosit.y + 0.5) + (y * (2.4 * picJizhunHeight))
    if (Boolean(storage.get("isSanguo", false))) {
        xx = (xiaodituPosit.x) + ((x) * (4.328 * picJizhun))
        yy = (xiaodituPosit.y) + (y * (3.652 * picJizhunHeight))
    }
    log('小地图屏幕点：', xx, yy)
    sleep(yanchi1)
    click(xx, yy)
    sleep(yanchi2)
}
